package templates

import (
	_ "embed"
	"text/template"
)

//go:embed chi/chi-handler.tmpl
var chiHandler string
//go:embed chi/chi-interface.tmpl
var chiInterface string
//go:embed chi/chi-middleware.tmpl
var chiMiddleware string

//go:embed eco/register.tmpl
var register string
//go:embed eco/server-interface.tmpl
var serverInterfaceTmpl string
//go:embed eco/wrappers.tmpl
var wrappers string

//go:embed client/client-with-responses.tmpl
var clientWithResponses string
//go:embed client/client.tmpl
var client string

//go:embed additional-properties.tmpl
var additionalProperties string


//go:embed imports.tmpl
var imports string
//go:embed inline.tmpl
var inline string
//go:embed param-types.tmpl
var paramTypes string

//go:embed request-bodies.tmpl
var requestBodies string

//go:embed typedef.tmpl
var typedef string

var templates = map[string]string{
	"additional-properties.tmpl": additionalProperties,
	"chi-handler.tmpl": chiHandler,
	"chi-interface.tmpl": chiInterface,
	"chi-middleware.tmpl": chiMiddleware,
	"client-with-responses.tmpl": clientWithResponses,
	"client.tmpl": client,
	"imports.tmpl": imports,
	"inline.tmpl": inline,
	"param-types.tmpl": paramTypes,
	"register.tmpl": register,
	"request-bodies.tmpl": requestBodies,
	"server-interface.tmpl": serverInterfaceTmpl,
	"typedef.tmpl": typedef,
	"wrappers.tmpl": wrappers,
}

// Parse parses declared templates.
func Parse(t *template.Template) (*template.Template, error) {
	for name, s := range templates {
		var tmpl *template.Template
		if t == nil {
			t = template.New(name)
		}
		if name == t.Name() {
			tmpl = t
		} else {
			tmpl = t.New(name)
		}
		if _, err := tmpl.Parse(s); err != nil {
			return nil, err
		}
	}
	return t, nil
}

